package cn.itcast.controller;


import cn.itcast.constant.MessageConstant;
import cn.itcast.entity.PageResult;
import cn.itcast.entity.QueryPageBean;
import cn.itcast.entity.Result;
import cn.itcast.pojo.Permission;
import cn.itcast.service.PermissionService;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

@RestController
@RequestMapping(path = "/permission")
public class PermissionController {

    @Reference
    private PermissionService permissionService;

    /**
     * 查找权限列表（可带参数搜索）
     *
     * @return {@link Result}
     */
    @GetMapping
    public Result findPermission(QueryPageBean queryPageBean){
        try {
            PageResult result = permissionService.findPermission(queryPageBean.getCurrentPage(), queryPageBean.getPageSize(), queryPageBean.getQueryString());
            return new Result(true, MessageConstant.QUERY_PERMISSION_SUCCESS, result);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_PERMISSION_FAIL);
        }
    }

    /**
     * 查找权限列表（可带参数搜索）
     *
     * @return {@link Result}
     */
    @GetMapping("all")
    public Result findAllPermission(){
        try {
            List<Permission> result = permissionService.findAllPermission();
            return new Result(true, MessageConstant.QUERY_PERMISSION_SUCCESS, result);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_PERMISSION_FAIL);
        }
    }

    /**
     * 添加权限
     *
     * @param permission 许可
     * @return {@link Result}
     */
    @PostMapping
    public Result addPermission(@RequestBody Permission permission){
        try {
            permissionService.addPermission(permission);
            return new Result(true, MessageConstant.ADD_PERMISSION_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_PERMISSION_FAIL);
        }
    }

    /**
     * 更新权限
     *
     * @param permission 许可
     * @return {@link Result}
     */
    @PutMapping
    public Result updatePermission(@RequestBody Permission permission){
        try {
           permissionService.updatePermission(permission);
            return new Result(true, MessageConstant.EDIT_PERMISSION_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_PERMISSION_FAIL);
        }
    }

    /**
     * 找到一个许可
     *
     * @param  id id
     * @return {@link Result}
     */
    @GetMapping("{id}")
    public Result findPermissionById(@PathVariable("id") Integer id){
        try {
            Permission result = permissionService.findPermissionById(id);
            return new Result(true, MessageConstant.QUERY_PERMISSION_SUCCESS, result);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_PERMISSION_FAIL);
        }
    }

    /**
     * 删除权限的id
     *
     * @param id id
     * @return {@link Result}
     */
    @DeleteMapping("{id}")
    public Result deletePermissionById(@PathVariable("id") Integer id){
        try {
            boolean bool = permissionService.deletePermissionById(id);
            if (bool){
                return new Result(true, MessageConstant.DELETE_PERMISSION_SUCCESS);
            }
            return new Result(false, MessageConstant.DELETE_PERMISSION_FAIL_1);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_PERMISSION_FAIL);
        }
    }

    @GetMapping("/menu/association")
    public Result findMenuAssociationPermission(@RequestParam("menuIds") Integer[] menuIds){
        try {
            Set<Permission> permissionList = permissionService.findMenuAssociationPermission(menuIds);
            return new Result(true, MessageConstant.DELETE_PERMISSION_SUCCESS,permissionList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_PERMISSION_FAIL);
        }
    }


}
